<?php

/*
 * Copyright 2017 zhc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


/**
 * Keep the display word in the database
 */
trait _display_words_db{
    public function GetDisplayString($dbstr) {
        return $dbstr;
    }  
}

/**
 * Keep the use input string 
 */
trait _filter_sql_string_orign{
    public function GetSQLString($object) {
        return core::get_safe_input([$object], [0], ['raw'])[0];
    }
}

/**
 * Keep the html code in the submit
 */
trait _filter_sql_html{
    public function GetSQLString($object) {
        return  core::get_safe_input([$object], [0], ['raw'])[0];
        
    }
}

/**
 * The user input should be an integer
 */
trait _filter_sql_int{
    public function GetSQLString($object) {
        return intval($object);
    }
}

/**
 * The unique book ID column
 */
class BookIDCol extends ColumnProperty{
    public function __construct() {
         parent::__construct();
         $this->dbcol = 'id';
         $this->readonly = true;
         $this->name = 'bid';
         $this->type = ColumnProperty::COL_PROP_INT;
         $this->display = _('Book ID');
         $this->description = _('The book unique id, one book on number');
    }

    use _display_words_db;
    use _filter_sql_int;
}

/**
 * The book name column
 */
class BookNameCol extends ColumnProperty{
    public function __construct() {
        $this->dbcol = 'name';
        $this->readonly = false;
        $this->name = 'bname';
        $this->type = ColumnProperty::COL_PROP_STRING;
        $this->display = _('Book Name');
        $this->description = _('The book name');
        parent::__construct();
    }
    use _display_words_db;
    use _filter_sql_string_orign;
}

/**
 * The book author column
 */
class BookAuthorCol extends ColumnProperty{
    
    public function __construct() {
        $this->dbcol = 'author';
        $this->readonly = false;
        $this->name = 'bkauthor';
        $this->type = ColumnProperty::COL_PROP_STRING;
        $this->display = _('Author');
        $this->description = _('The authors of the book, separate with comma');
        parent::__construct();
    }
    use _display_words_db;
    use _filter_sql_string_orign;
}

/**
 * The book publisher column
 */
class BookPubCol extends ColumnProperty{
    
    public function __construct() {
        $this->dbcol = 'publisher';
        $this->readonly = false;
        $this->name = 'pubname';
        $this->type = ColumnProperty::COL_PROP_STRING;
        $this->display = _('Publisher');
        $this->description = _('The publishing house');
        parent::__construct();
    }
    
    use _display_words_db;
    use _filter_sql_string_orign;

}

/**
 * The book ISBN column
 */
class BookIsbnCol extends ColumnProperty{
    public function __construct() {
        $this->dbcol = 'isbn';
        $this->readonly = false;
        $this->name = 'bkisbn';
        $this->type = ColumnProperty::COL_PROP_STRING;
        $this->display = _('ISBN');
        $this->description = _('ISBN in 13 digits');
        parent::__construct();
    }
    
    /**
     * Remove all the '-' in the ISBN code
     * @param string $object
     * @return string
     */
    public function GetSQLString($object) {
        $isbn = str_replace('-', '', $object);
        return core::get_safe_input([$isbn], [0], ['idecimal'])[0];
    }

    use _display_words_db;
    
}

/**
 * The book price column
 */
class BookPriceCol extends ColumnProperty{
    public function __construct() {
        $this->dbcol = 'price';
        $this->readonly = false;
        $this->name = 'bkprice';
        $this->type = ColumnProperty::COL_PROP_STRING;
        $this->display = _('Price');
        $this->description = _('Price without the currency number');
        parent::__construct();
    }
    use _display_words_db;
    public function GetSQLString($object) {
        return core::get_safe_input([$object], [0], ['float'])[0];
    }  
}

/**
 * The book description column
 */
class BookDescriptionCol extends ColumnProperty{
    public function __construct() {
        $this->dbcol = 'description';
        $this->readonly = false;
        $this->name = 'bkdesc';
        $this->type = ColumnProperty::COL_PROP_STRING;
        $this->display = _('Description');
        $this->description = _('A short abstract of this book');
        parent::__construct();
    }
    use _display_words_db;
    use _filter_sql_string_orign;
}

/**
 * The book rent out column
 */
class BookRentCol extends ColumnProperty{
    public function __construct() {
        $this->dbcol = 'rent';
        $this->readonly = true;
        $this->name = 'rent';
        $this->type = ColumnProperty::COL_PROP_STRING;
        $this->display = _('Rent out ');
        $this->description = _('The rent out user');
        parent::__construct();
    }
    public function GetDisplayString($dbstr) {
        return $dbstr;
    }
    public function GetSQLString($object) {
        return core::get_safe_input([$object], [0], ['int'])[0];
    }
    
}

